#pragma once

#include<iostream>
#include<assert.h>
#include<string>
using namespace std;

namespace rtx
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void push_back(const T& x)
		{
			//if (_finish == _end_of_storage)
			//{
			//	reserve(capacity() == 0 ? 4 : capacity() * 2);
			//}
			//*_finish = x;
			//++_finish;
			insert(end(), x);
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if(_start)
				{
					//memcpy(tmp, _start, sizeof(T) * sz); //浅拷贝，不行

					for (size_t i = 0; i < sz; i++)// 如果T是int，一个一个拷贝没问题
					{
						tmp[i] = _start[i];// 如果T是string等自定义问题，一个一个拷贝调用的是T的深拷贝，也不会出问题。
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = tmp + sz;
				_end_of_storage = tmp + n;
			}
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return *(_start + pos);
		}
		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return *(_start + pos);
		}

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		template<class InputInterator>
		vector(InputInterator first, InputInterator last)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n > capacity())
			{
				reserve(n);
			}
			if (n > size())
			{
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		void pop_back() 
		{
			assert(_finish > _start);
			--_finish;
		}

		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= _start);// ①检查pos是否越界
			assert(pos <= _finish);

			if (_finish == _end_of_storage)// ②检查是否需要扩容
			{
				size_t len = pos - _start;// 记录一下pos到_start的距离
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;// 迭代器失效问题，扩容后pos还是指向原来的空间，更新一下pos，
				//而且形参不会影响实参，传引用的话begin等就传不了，所以用返回解决
			}

			iterator right = _finish - 1;// ③移动数据
			while (right >= pos)
			{
				*(right + 1) = *right;
				--right;
			}

			*pos = val;// ④插入数据
			++_finish;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);// 不能<= 因为_finish指向的是最后一个数据的下一个

			iterator left = pos + 1;
			while (left < _finish)
			{
				*(left - 1) = *left;
				++left;
			}
			--_finish;
			return pos;//此时pos就是删除位置下一个位置迭代器
		}

		//vector(const vector<T>& v)// 传统写法
		//{
		//	reserve(v.capacity());
		//	// memcpy(_start, v._start, v.size() * sizeof(T));  // 会翻车
		//	for (const auto& e : v)
		//	{
		//		push_back(e);
		//	}
		//}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector(const vector<T>& v)// 现代写法
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}

		vector<T>& operator=(vector<T> v)// 现代写法
		{
			swap(v);
			return *this;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}
